﻿using System;

namespace Avalon.Core.Data
{
    public static class RepositoryConfig
    {
        private static Type _repositoryType;
        public static void RegisterRepository(Type repositoryType)
        {
            _repositoryType = repositoryType;
        }

        public static IRepository<T> Initialize<T>() where T : class, IEntity, new()
        {
            return (IRepository<T>)Activator.CreateInstance(_repositoryType.MakeGenericType(typeof(T)));
        }

        public static IRepository Initialize(Type targetType)
        {
            return (IRepository)Activator.CreateInstance(_repositoryType.MakeGenericType(targetType));
        }

        public static IRepository Initialize()
        {
            if (_repositoryType == null) throw new NullReferenceException("Repository type can never be null.");
            return (IRepository)Activator.CreateInstance(_repositoryType);
        }
    }
}
